package conceptmaniax.hotelapp.adapters;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import conceptmaniax.hotelapp.activities.R;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.util.image.ImageUtils;

public class PhotoFragment extends Fragment {
	public Activity activity;
	private ImageView scrollImage;
	
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.myfragment_layout, container, false);
        scrollImage = (ImageView)rootView.findViewById(R.id.photo);
        SetImages();
        return rootView;
    }
    public void SetImages() {
        new SetImagesTask(scrollImage).execute();
    }

	private class SetImagesTask extends AsyncTask<Void, Void, Void> {
		ImageView img;
		public SetImagesTask(ImageView im) {
			this.img = im;
		}

		@Override
		protected Void doInBackground(Void... params) {
			return null;
		}
		
		@Override
	    protected void onPostExecute(Void re) {
			// scale layout width, keep layout height constant
			float widthPx = (float) activity.getResources().getDisplayMetrics().widthPixels;
			Bundle bundle=getArguments(); 
			String result = bundle.getString("name"); 
			int position = bundle.getInt("position");
			BitmapFactory.Options options = ImageUtils.getOptionsFromImagesFile(activity, Constants.IMAGES_FOLDER + File.separator + result + "_" + position + ".jpg");
			final int mainBitMapWidth = options.outWidth;
			final int mainBitMapHeight = options.outHeight;
			File scrollImageFile = new File(activity.getFilesDir().getAbsolutePath() + File.separator + Constants.IMAGES_FOLDER + File.separator + Constants.IMAGES_FOLDER + File.separator + result + "_" +position + ".jpg");
						
			// check if file exists
			if (scrollImageFile.exists()){
				// fix options		
				FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams((int) (widthPx), (int) (widthPx * ImageUtils.ImageRatioI(mainBitMapWidth, mainBitMapHeight)));
				img.setLayoutParams(layoutParams);
				options.inSampleSize = ImageUtils.getScale(mainBitMapWidth, mainBitMapHeight, layoutParams.width, layoutParams.height);
				options.inJustDecodeBounds = false;
				options.inPurgeable = true;
				options.inInputShareable = true;
				img.setAdjustViewBounds(true);
				img.setScaleType(ScaleType.FIT_CENTER);
		        FileInputStream fs = null;
				try {
					fs = new FileInputStream(scrollImageFile);
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					Bitmap baseBitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
					img.setImageBitmap(baseBitmap);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
					Log.i("MESSAGE","DOES NOT EXIST");
				}
			}
		}
}
